#include "ResultView.h"

namespace Xps
{
	ResultView::ResultView(XpsAssets::AssetManager* am, QWidget* parent) : QWidget(parent)
	{
		this->am = am;
		rm = new ResultModel(am);
		
		QVBoxLayout* layout = new QVBoxLayout();
		QHBoxLayout* editor_layout = new QHBoxLayout();
		QHBoxLayout* input_layout = new QHBoxLayout();
		layout->setMargin(0);
		value = new QLineEdit();
		tolerance = new QLineEdit();
		frame = new QFrame();
		frame->setStyleSheet(".QFrame { border: 1px solid gray; border-radius: 5px; padding: 2px; padding-left: 5px; padding-right: 5px; }");
		exact = new QPushButton(QIcon(":/rs/images/exact.png"),"Exact");
		range = new QPushButton(QIcon(":/rs/images/range.png"),"Range");
		fade = new QPushButton(QIcon(":/rs/images/fade.png"),"Fade");
		ok = new QPushButton(QIcon(":/rs/images/ok.png"), "");
		setLayout(layout);
		table = new QTableView();
		attributeeditor = new QWidget();
		attributeeditor->setLayout(editor_layout);
		input_layout->addWidget(new QLabel("Value:"));
		input_layout->addWidget(value);
		input_layout->addWidget(new QLabel("Tolerance:"));
		input_layout->addWidget(tolerance);
		input_layout->addWidget(ok);
		input_layout->setMargin(0);
		frame->setLayout(input_layout);
		editor_layout->addWidget(frame);
		editor_layout->addWidget(exact);
		editor_layout->addWidget(range);
		editor_layout->addWidget(fade);
		layout->addWidget(attributeeditor);
		layout->addWidget(table);
		
		table->setModel(rm);
		table->setAlternatingRowColors(true);
		table->setShowGrid(false);

		attributeeditor->setDisabled(true);
		selectionmodel = table->selectionModel();

		connect(am, SIGNAL(sNewResult(int)), rm, SLOT(addResult(int)));
		connect(am, SIGNAL(sNewAttribute(int)), rm, SLOT(addAttribute(int)));
		connect(am, SIGNAL(sAttributeRemoved(int)), rm, SLOT(remAttribute(int)));
		connect(am, SIGNAL(sResultRemoved(int)), rm, SLOT(remResult(int)));
		connect(selectionmodel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(selectionChanged(QItemSelection,QItemSelection)));

		connect(exact, SIGNAL(clicked()), this, SLOT(makeExact()));
		connect(fade, SIGNAL(clicked()), this, SLOT(makeFade()));
		connect(range, SIGNAL(clicked()), this, SLOT(makeRange()));
		connect(ok, SIGNAL(clicked()), this, SLOT(set()));
		connect(value, SIGNAL(returnPressed()), ok, SIGNAL(clicked()));
		connect(tolerance, SIGNAL(returnPressed()), ok, SIGNAL(clicked()));
	}

	void ResultView::set()
	{
		QList<QModelIndex> selection = selectionmodel->selectedIndexes();
	
		for (int i = 0; i < selection.count(); i++)
		{
			if (value->text() != "")
				rm->setData(selection.at(i), value->text().toInt(), ResultModel::Role::VALUE);
			
			if (tolerance->text() != "")
				rm->setData(selection.at(i), tolerance->text().toInt(), ResultModel::Role::TOLERANCE);
		}
	}

	void ResultView::makeExact()
	{
		QList<QModelIndex> selection = selectionmodel->selectedIndexes();
	
		for (int i = 0; i < selection.count(); i++)
		{
			rm->setData(selection.at(i), XpsAssets::MatchMode::EXACT, ResultModel::Role::MATCHMODE);
		}
	}

	void ResultView::makeRange()
	{
		QList<QModelIndex> selection = selectionmodel->selectedIndexes();
	
		for (int i = 0; i < selection.count(); i++)
		{
			rm->setData(selection.at(i), XpsAssets::MatchMode::RANGE, ResultModel::Role::MATCHMODE);
		}
	}

	void ResultView::makeFade()
	{
		QList<QModelIndex> selection = selectionmodel->selectedIndexes();
	
		for (int i = 0; i < selection.count(); i++)
		{
			rm->setData(selection.at(i), XpsAssets::MatchMode::FADE, ResultModel::Role::MATCHMODE);
		}
	}

	void ResultView::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
	{
		QList<QModelIndex> selection = selectionmodel->selectedIndexes();

		qDebug() << selection.count();

		if (selection.count() == 0)
			attributeeditor->setDisabled(true);
		else
			attributeeditor->setDisabled(false);

		tolerance->setEnabled(true);	

		if (selection.count() == 1)
		{
			value->setText(rm->data(selection.at(0), ResultModel::Role::VALUE).toString());
			tolerance->setText(rm->data(selection.at(0), ResultModel::Role::TOLERANCE).toString());


			if (rm->data(selection.at(0), ResultModel::Role::MATCHMODE) == XpsAssets::MatchMode::EXACT)
			{
				tolerance->setDisabled(true);
			}
			else if (rm->data(selection.at(0), ResultModel::Role::MATCHMODE) ==  XpsAssets::MatchMode::RANGE)
			{
			}
			else if (rm->data(selection.at(0), ResultModel::Role::MATCHMODE) == XpsAssets::MatchMode::FADE)
			{
			}
		}
		else if (selection.count() > 1)
		{
			value->setText("");
			tolerance->setText("");
		}
	}
}